package top.toptimus.dto.stock.purchase;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

/**
 * 采购入库和其他入库DTO
 * TODO 缺字段，如订单凭证等
 *
 * @author gaoyu
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PurchaseOrderDTO {
    private String orderId; //  采购订单id
    private Date orderDate; //  采购时间
    private Date purchaseStockInDate;   //  采购入库时间
    private Double totalPriceWithTax;   //  含税价格
    private Double transitExpenses = 0.0; //  在途费用
    private List<PurchaseOrderEntryDTO> purchaseOrderEntryDTOS;

    /**
     * 核算入库加权平均价格
     *
     * @return this
     */
    public PurchaseOrderDTO build() {
        //  总数量
        double totolQuantity = purchaseOrderEntryDTOS.stream().mapToDouble(PurchaseOrderEntryDTO::getQuantity).sum();
        for (PurchaseOrderEntryDTO purchaseOrderEntryDTO : purchaseOrderEntryDTOS) {
            purchaseOrderEntryDTO.setStockInUnitPrice(
                    purchaseOrderEntryDTO.getPurchasePrice() + (this.transitExpenses * (purchaseOrderEntryDTO.getQuantity() / totolQuantity))
            );  //  入库加权平均价格计入在途费用
        }
        return this;
    }
}
